package two;

public class OneHundred_OneHundredAndOne {

    // 100. 查找两个树是否相等
    public boolean isSameTree(TreeNode p, TreeNode q){
        boolean result =false;
        if (p == null && q == null) {
            result = true;
        } else if (p == null || q == null || p.val != q.val) {
            // if ((p == null && q != null) || (p != null && q == null))
            // 两种写法，能看懂哪种看哪种吧 短的是idea优化的，不能不感叹idea功能强大
            // 左右节点其中一个为null
        } else {
            result = isSameTree(p.right, q.right) && isSameTree(p.left, q.left);
        }
        return result;
    }
    // 101.对称二叉树
    public boolean isSymmetric(TreeNode root) {
        // 为空 返回true
        if (root == null)
            return true;
        return check(root.right, root.left);
    }

    private boolean check(TreeNode right, TreeNode left) {
        boolean result = false;
        // 都为null 返回true
        if (right == null && left == null) {
            result = true;
        } else if (right == null || left == null || right.val != left.val) {
            // if ((right == null && left != null) || (right != null && left == null))
            // 两种写法，能看懂哪种看哪种吧 短的是idea优化的，不能不感叹idea功能强大
            // 左右节点其中一个为null
        } else {
            result = check(right.right, left.left) && check(right.left, left.right);
        }
        return result;
    }
    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
